﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Net;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace MyDiary
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Information : Window
    {
        BackgroundWorker worker = new BackgroundWorker();

        public Information()
        {
            InitializeComponent();
            version.Text = "Version : " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            worker = new BackgroundWorker();
            worker.WorkerSupportsCancellation = true;
            worker.DoWork += new DoWorkEventHandler(MdbDataImport_importing);
            worker.RunWorkerAsync();

        }

        void MdbDataImport_importing(object sender, EventArgs e)
        {
            progressBar1.Dispatcher.Invoke(
                System.Windows.Threading.DispatcherPriority.Normal,
                new Action(delegate() { progressBar1.Visibility = Visibility.Visible; progressBar1.Value = 10; }));

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://www.ithikari.com/projects/mydiary.txt");
            
            Thread.Sleep(500);
            
            progressBar1.Dispatcher.Invoke(
                  System.Windows.Threading.DispatcherPriority.Normal,
                  new Action(delegate() { progressBar1.Value = 40; }));
            Thread.Sleep(500);
            
            string responseData = string.Empty;
            
            HttpWebResponse httpResponse = (HttpWebResponse)webRequest.GetResponse();
            
            using (StreamReader responseReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                responseData = responseReader.ReadToEnd();
            }
            
            progressBar1.Dispatcher.Invoke(
                  System.Windows.Threading.DispatcherPriority.Normal,
                  new Action(delegate() { progressBar1.Value = 90; }));
            
            Thread.Sleep(500);
            
            progressBar1.Dispatcher.Invoke(
                  System.Windows.Threading.DispatcherPriority.Normal,
                  new Action(delegate() { progressBar1.Value = 100; progressBar1.Visibility = Visibility.Collapsed; }));

            if (!responseData.Equals(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()))
            {
                btnDownload.Dispatcher.Invoke(
                  System.Windows.Threading.DispatcherPriority.Normal,
                  new Action(delegate() { btnDownload.Visibility = Visibility.Visible; }));

            }
            else {
                MessageBox.Show("There are no updates avaliable");
            }

        }

        private void btnGo_Click(object sender, RoutedEventArgs e)
        {
            Process.Start("http://www.ithikari.com");
        }

        private void btnDownload_Click(object sender, RoutedEventArgs e)
        {
            Process.Start("http://mydriady.codeplex.com/");
            
        }
    }
}
